import { mapState } from "vuex";
import state from "@/tools/state";
import ruleBaseAPI from "@/api/ruleBaseApi.js";

export const mixin = {
  data() {
    return {
      selectData: [],
      formData: {}
    }
  },
  computed: {
    ...mapState('sasspWeb', state)
  },
  mounted() {
    this.formData = this.searchData
  },
  watch: {
    selectData: {
      handler(val) {
        this.formData =val
      },
      deep: true,
      immediate:true
    }
  },

  methods: {
    //默认规则状态样式显示
    stateHandle(h, row) {
      // console.log(row.state, '---', row.stateStr)
      return h(
          "StatusButton",
          {
            props: {
              // status: row.state
              status:row.state === 1 ? 0 : 1
            }
          },
          row.stateStr
        )
    },

    //流程状态样式显示
    bpmn_stateHandle(h, row) {
      // console.log('流程状态样row', row.bpmnState);
      return h('span',
        {
          class: {
            pass: row.bpmnState == 2||row.bpmnState == 3,
            warning: row.bpmnState == 1,
            fail: row.bpmnState == 0
          }
        },
        [
          h('span',
          {
            class: 'circle'
          }
          ),
          row.taskName?row.taskName:row.bpmnStateStr
        ]
      )
    },
    //标签选择
    label_Handle(h, row) {
      // console.log('label', row.label);
      return h('span',
        {
          class: {
            pass: row.label,
            // warning: row.lable == 1,
            // fail: row.lable == 0
          }
        },
        [
          h('span',
          {
            class: 'circle'
          }
          ),
          row.labelStr
        ]
      )
    },
    // 校验类型checkTypeHandle
    checkTypeHandle(h, row) {
      // console.log(row, '---', row.checkType)
      return h(
        'span',
        {
          class: {
            pass: row.checkType == 2,
            warning: row.checkType == 1,
            fail: row.checkType == 0
          }
        },
        [h('span', {
          class:'circle'
        }),
        row.checkTypeStr? row.checkTypeStr :''
        ]
      )
    },


    //删除事件
    deleteHandle(pro) {
      let allSelected = this.$refs["listTable"].allSelection;
      if (allSelected.length == 0) {
        this.$message.error(this.$t("请至少选择一条数据"));
        return;
      }
      this.$confirm
        .warning(this.$t("确定删除后，无法恢复！"))
        .then(() => {
          const idList = allSelected.map(user => user.id);
          if (pro == '子表') {
            // 删除子表
            ruleBaseAPI.deleteDetails({idList}).then(res => {
              this.$message.success({
                content: this.$t("删除成功"),
                duration: 3
              });
                this.$refs["listTable"].allSelection = [];
                this.searchHandle()
              });
          } else {
            // 删除
            ruleBaseAPI.deleteData({idList}).then(res => {
              this.$message.success({
                content: this.$t("删除成功"),
                duration: 3
              });
              this.$refs["listTable"].allSelection = [];
              this.searchHandle()
            });
          }
        }).catch(_ => _);
    },

    //查找事件按钮
    searchHandle() {
      const params = {};
      this.formData.forEach(item => {
          params[item.field] = item.value;
      });
      this.$refs.listTable.load(params);
    },
    //重置事件按钮
    resetHandle() {
      this.formData.forEach(item => {
        item.value = null;
      });
      this.$refs.listTable.load(this.pageTableConfig.initParam);
    },
    selectHandle(data) {
      this.selectData = data.map(item => item.id)
    },
  },
}
